﻿using Abp.Authorization;
using Abp.Localization;
using Abp.MultiTenancy;
using Abp.Net.Mail;
using Akso.Authorization;
using Akso.Authorization.Roles;
using Akso.Authorization.Users;
using Akso.Editions;
using FluentMigrator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Akso.Migrations
{
    /// <summary>
    /// 将AspNetZero的SQL Server迁移类在MySql中实现，本实例支持到SQL Server的201605051327136版本。
    /// </summary>
    [Migration(201605051327136)]
    public class From_0_To_201605051327136_ASPNETZERO : AutoReversingMigration
    {
        public override void Up()
        {
            //**************************Table & Index*****************************
            #region Table & Index

            Create.Table("dbo.AbpUsers")
                   .WithIdColumnInt64()
                   .WithColumn("TenantId").AsInt64().Nullable()
                   .WithColumn("Name").AsString(32).NotNullable()
                   .WithColumn("Surname").AsString(32).NotNullable()
                   .WithColumn("UserName").AsString(32).NotNullable()
                   .WithColumn("Password").AsString(128).NotNullable()
                   .WithColumn("EmailAddress").AsString(255).NotNullable()
                   .WithColumn("IsEmailConfirmed").AsBoolean().NotNullable()
                   .WithColumn("EmailConfirmationCode").AsString(128).Nullable()
                   .WithColumn("PasswordResetCode").AsString(328).Nullable()
                   .WithColumn("LastLoginTime").AsDateTime().Nullable()
                   .WithColumn("IsActive").AsBoolean().NotNullable().WithDefaultValue(true)
                   .WithColumn("ShouldChangePasswordOnNextLogin").AsBoolean().NotNullable().WithDefaultValue(false)
                   .WithColumn("ProfilePictureId").AsString(36).Nullable()
                   .WithColumn("AuthenticationSource").AsString(64).Nullable()
                   .WithAuditColumns();
            Create.Index("IX_dbo.AbpUsers_CreatorUserId").OnTable("dbo.AbpUsers").OnColumn("CreatorUserId")
                .Ascending().WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpUsers_DeleterUserId").OnTable("dbo.AbpUsers").OnColumn("DeleterUserId")
                .Ascending().WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpUsers_IsDeleted_TenantId_EmailAddress").OnTable("dbo.AbpUsers")
                .OnColumn("IsDeleted").Ascending()
                .OnColumn("TenantId").Ascending()
                .OnColumn("EmailAddress").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpUsers_IsDeleted_TenantId_UserName").OnTable("dbo.AbpUsers")
                .OnColumn("IsDeleted").Ascending()
                .OnColumn("TenantId").Ascending()
                .OnColumn("UserName").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpUsers_LastModifierUserId").OnTable("dbo.AbpUsers").OnColumn("LastModifierUserId")
                .Ascending().WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpUsers_TenantId_EmailAddress").OnTable("dbo.AbpUsers")
                .OnColumn("TenantId").Ascending()
                .OnColumn("EmailAddress").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpUsers_TenantId_UserName").OnTable("dbo.AbpUsers")
                .OnColumn("TenantId").Ascending()
                .OnColumn("UserName").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpUserLogins")
                .WithIdColumnInt64()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("UserId").AsInt64().NotNullable()
                .WithColumn("LoginProvider").AsString(128).NotNullable()
                .WithColumn("ProviderKey").AsString(256).NotNullable();
            Create.Index("IX_dbo.AbpUserLogins_UserId_LoginProvider").OnTable("dbo.AbpUserLogins")
                .OnColumn("UserId").Ascending()
                .OnColumn("LoginProvider").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpRoles")
                .WithIdColumnInt64()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("Name").AsString(32).NotNullable()
                .WithColumn("DisplayName").AsString(64).NotNullable()
                .WithColumn("IsStatic").AsBoolean().NotNullable().WithDefaultValue(false)
                .WithColumn("IsDefault").AsBoolean().NotNullable().WithDefaultValue(false)
                .WithAuditColumns();
            Create.Index("IX_dbo.AbpRoles_CreatorUserId").OnTable("dbo.AbpRoles").OnColumn("CreatorUserId")
                .Ascending().WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpRoles_DeleterUserId").OnTable("dbo.AbpRoles").OnColumn("DeleterUserId")
                .Ascending().WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpRoles_IsDeleted_TenantId_Name").OnTable("dbo.AbpRoles")
                .OnColumn("IsDeleted").Ascending()
                .OnColumn("TenantId").Ascending()
                .OnColumn("Name").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpRoles_LastModifierUserId").OnTable("dbo.AbpRoles").OnColumn("LastModifierUserId")
                .Ascending().WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpRoles_TenantId_Name").OnTable("dbo.AbpRoles")
                .OnColumn("TenantId").Ascending()
                .OnColumn("Name").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpUserRoles")
                .WithIdColumnInt64()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("UserId").AsInt64().NotNullable()
                .WithColumn("RoleId").AsInt64().NotNullable()
                .WithCreateColumns();
            Create.Index("IX_dbo.AbpUserRoles_RoleId").OnTable("dbo.AbpUserRoles").OnColumn("RoleId")
                .Ascending().WithOptions().NonClustered(); ;
            Create.Index("IX_dbo.AbpUserRoles_UserId_RoleId").OnTable("dbo.AbpUserRoles")
                .OnColumn("UserId").Ascending()
                .OnColumn("RoleId").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpPermissions")
                .WithIdColumnInt64()
                .WithColumn("Name").AsString(128).NotNullable()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("RoleId").AsInt64().Nullable()
                .WithColumn("UserId").AsInt64().Nullable()
                .WithColumn("IsGranted").AsBoolean().NotNullable()
                .WithColumn("Discriminator").AsString(128).NotNullable()
                .WithAuditColumns();
            Create.Index("IX_dbo.AbpPermissions_RoleId_Name").OnTable("dbo.AbpPermissions")
                .OnColumn("RoleId").Ascending()
                .OnColumn("Name").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpPermissions_UserId_Name").OnTable("dbo.AbpPermissions")
                .OnColumn("UserId").Ascending()
                .OnColumn("Name").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpSettings")
                .WithIdColumnInt64()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("UserId").AsInt64().Nullable()
                .WithColumn("Name").AsString(255).NotNullable()
                .WithColumn("Value").AsString(2000).NotNullable()
                .WithAuditColumns();
            Create.Index("IX_dbo.AbpSettings_TenantId_Name").OnTable("dbo.AbpSettings")
                .OnColumn("TenantId").Ascending()
                .OnColumn("Name").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpSettings_UserId_Name").OnTable("dbo.AbpSettings")
                .OnColumn("UserId").Ascending()
                .OnColumn("Name").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpTenants")
                .WithIdColumnInt64()
                .WithColumn("TenancyName").AsString(64).NotNullable()
                .WithColumn("Name").AsString(128).NotNullable()
                .WithColumn("EditionId").AsInt64().Nullable()
                .WithColumn("ConnectionString").AsString(1024).Nullable()
                .WithAuditColumns();
            Create.Index("IX_dbo.AbpTenants_CreatorUserId").OnTable("dbo.AbpTenants")
                .OnColumn("CreatorUserId").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpTenants_DeleterUserId").OnTable("dbo.AbpTenants")
                .OnColumn("DeleterUserId").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpTenants_EditionId").OnTable("dbo.AbpTenants")
                .OnColumn("EditionId").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpTenants_IsDeleted_TenancyName").OnTable("dbo.AbpTenants")
                .OnColumn("IsDeleted").Ascending()
                .OnColumn("TenancyName").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpTenants_LastModifierUserId").OnTable("dbo.AbpTenants")
                .OnColumn("LastModifierUserId").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpTenants_TenancyName").OnTable("dbo.AbpTenants")
                .OnColumn("TenancyName").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpEditions")
                .WithIdColumnInt64()
                .WithColumn("Name").AsString(32).NotNullable()
                .WithColumn("DisplayName").AsString(64).NotNullable()
                .WithAuditColumns();
            Create.Index("IX_dbo.AbpEditions_Name").OnTable("dbo.AbpEditions")
                .OnColumn("Name").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpFeatures")
                .WithIdColumnInt64()
                .WithColumn("EditionId").AsInt64().Nullable()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("Name").AsString(128).NotNullable()
                .WithColumn("Value").AsString(2000).NotNullable()
                .WithColumn("Discriminator").AsString(128).NotNullable()
                .WithAuditColumns();
            Create.Index("IX_dbo.AbpFeatures_Discriminator_EditionId_Name").OnTable("dbo.AbpFeatures")
                .OnColumn("Discriminator").Ascending()
                .OnColumn("EditionId").Ascending()
                .OnColumn("Name").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpFeatures_Discriminator_TenantId_Name").OnTable("dbo.AbpFeatures")
                .OnColumn("Discriminator").Ascending()
                .OnColumn("TenantId").Ascending()
                .OnColumn("Name").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpFeatures_TenantId").OnTable("dbo.AbpFeatures")
                .OnColumn("TenantId").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpFeatures_TenantId_Name").OnTable("dbo.AbpFeatures")
                .OnColumn("TenantId").Ascending()
                .OnColumn("Name").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpAuditLogs")
                .WithIdColumnInt64()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("UserId").AsInt64().Nullable()
                .WithColumn("ServiceName").AsString(256).Nullable()
                .WithColumn("MethodName").AsString(256).Nullable()
                .WithColumn("Parameters").AsString(1024).Nullable()
                .WithColumn("ExecutionTime").AsDateTime().NotNullable()
                .WithColumn("ExecutionDuration").AsInt32().NotNullable()
                .WithColumn("ClientIpAddress").AsString(64).Nullable()
                .WithColumn("ClientName").AsString(128).Nullable()
                .WithColumn("BrowserInfo").AsString(256).Nullable()
                .WithColumn("Exception").AsString(2000).Nullable()
                .WithColumn("ImpersonatorUserId").AsInt64().Nullable()
                .WithColumn("ImpersonatorTenantId").AsInt64().Nullable()
                .WithColumn("CustomData").AsString(2000).Nullable();
            Create.Index("IX_dbo.AbpAuditLogs_TenantId_ExecutionTime").OnTable("dbo.AbpAuditLogs")
                .OnColumn("TenantId").Ascending()
                .OnColumn("ExecutionTime").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpAuditLogs_UserId_ExecutionTime").OnTable("dbo.AbpAuditLogs")
                .OnColumn("UserId").Ascending()
                .OnColumn("ExecutionTime").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpBackgroundJobs")
                .WithIdColumnInt64()
                .WithColumn("JobType").AsString(512).NotNullable()
                .WithColumn("JobArgs").AsMaxString().NotNullable()
                .WithColumn("TryCount").AsInt16().NotNullable()
                .WithColumn("NextTryTime").AsDateTime().NotNullable()
                .WithColumn("LastTryTime").AsDateTime().Nullable()
                .WithColumn("IsAbandoned").AsBoolean().NotNullable()
                .WithColumn("Priority").AsByte().NotNullable()
                .WithCreateAndDeleteColumns();
            Create.Index("IX_dbo.AbpBackgroundJobs_IsAbandoned_NextTryTime").OnTable("dbo.AbpBackgroundJobs")
                .OnColumn("IsAbandoned").Ascending()
                .OnColumn("NextTryTime").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpLanguages")
                .WithIdColumnInt64()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("Name").AsString(10).Nullable()
                .WithColumn("DisplayName").AsString(64).NotNullable()
                .WithColumn("Icon").AsString(128).NotNullable()
                .WithAuditColumns();
            Create.Index("IX_dbo.AbpLanguages_TenantId_Name").OnTable("dbo.AbpLanguages")
                .OnColumn("TenantId").Ascending()
                .OnColumn("Name").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpLanguageTexts")
                .WithIdColumnInt64()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("LanguageName").AsString(10).Nullable()
                .WithColumn("Source").AsString(128).Nullable()
                .WithColumn("Key").AsString(255).NotNullable()
                .WithColumn("Value").AsMaxString().NotNullable()
                .WithAuditColumns();
            Create.Index("IX_dbo.AbpLanguageTexts_TenantId_LanguageName_Source_Key").OnTable("dbo.AbpLanguageTexts")
                .OnColumn("TenantId").Ascending()
                .OnColumn("LanguageName").Ascending()
                .OnColumn("Source").Ascending()
                .OnColumn("Key").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpNotifications")
                .WithIdColumnGuid()
                .WithColumn("NotificationName").AsString(96).NotNullable()
                .WithColumn("Data").AsMaxString().Nullable()
                .WithColumn("DataTypeName").AsString(512).Nullable()
                .WithColumn("EntityTypeName").AsString(250).Nullable()
                .WithColumn("EntityTypeAssemblyQualifiedName").AsString(512).Nullable()
                .WithColumn("EntityId").AsString(96).Nullable()
                .WithColumn("Severity").AsByte().NotNullable()
                .WithColumn("UserIds").AsMaxString().Nullable()
                .WithColumn("TenantIds").AsMaxString().Nullable()
                .WithColumn("ExcludedUserIds").AsMaxString().Nullable()
                .WithCreateColumns();

            Create.Table("dbo.AbpNotificationSubscriptions")
                .WithIdColumnGuid()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("UserId").AsInt64().NotNullable()
                .WithColumn("NotificationName").AsString(96).Nullable()
                .WithColumn("EntityTypeName").AsString(250).Nullable()
                .WithColumn("EntityTypeAssemblyQualifiedName").AsString(512).Nullable()
                .WithColumn("EntityId").AsString(96).Nullable()
                .WithCreateColumns();
            Create.Index("IX_dbo.AbpNotiSub_NotiName_TypeName_EntityId_UserId").OnTable("dbo.AbpNotificationSubscriptions")
                .OnColumn("NotificationName").Ascending()
                .OnColumn("EntityTypeName").Ascending()
                .OnColumn("EntityId").Ascending()
                .OnColumn("UserId").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpOrganizationUnits")
                .WithIdColumnInt64()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("ParentId").AsInt64().Nullable()
                .WithColumn("Code").AsString(128).NotNullable()
                .WithColumn("DisplayName").AsString(128).NotNullable()
                .WithCreateColumns();
            Create.Index("IX_dbo.AbpOrganizationUnits_ParentId").OnTable("dbo.AbpOrganizationUnits")
                .OnColumn("ParentId").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpOrganizationUnits_TenantId_Code").OnTable("dbo.AbpOrganizationUnits")
                .OnColumn("TenantId").Ascending()
                .OnColumn("Code").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpOrganizationUnits_TenantId_ParentId").OnTable("dbo.AbpOrganizationUnits")
                .OnColumn("TenantId").Ascending()
                .OnColumn("ParentId").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpTenantNotifications")
                .WithIdColumnGuid()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("NotificationName").AsString(96).NotNullable()
                .WithColumn("Data").AsMaxString().Nullable()
                .WithColumn("DataTypeName").AsString(512).Nullable()
                .WithColumn("EntityTypeName").AsString(250).Nullable()
                .WithColumn("EntityTypeAssemblyQualifiedName").AsString(512).Nullable()
                .WithColumn("EntityId").AsString(96).Nullable()
                .WithColumn("Severity").AsByte().NotNullable()
                .WithCreateColumns();

            Create.Table("dbo.AbpUserAccounts")
                .WithIdColumnInt64()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("UserId").AsInt64().NotNullable()
                .WithColumn("UserLinkId").AsInt64().Nullable()
                .WithColumn("UserName").AsMaxString().Nullable()
                .WithColumn("EmailAddress").AsMaxString().Nullable()
                .WithColumn("LastLoginTime").AsDateTime().Nullable()
                .WithAuditColumns();

            Create.Table("dbo.AbpUserLoginAttempts")
                .WithIdColumnInt64()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("UserId").AsInt64().Nullable()
                .WithColumn("TenancyName").AsString(64).Nullable()
                .WithColumn("UserNameOrEmailAddress").AsString(255).Nullable()
                .WithColumn("ClientIpAddress").AsString(64).Nullable()
                .WithColumn("ClientName").AsString(128).Nullable()
                .WithColumn("BrowserInfo").AsString(256).Nullable()
                .WithColumn("Result").AsByte().NotNullable()
                .WithColumn("CreationTime").AsDateTime().NotNullable();
            Create.Index("IX_dbo.AbpUserLoginAttempts_TenName_UserAddress_Result").OnTable("dbo.AbpUserLoginAttempts")
                .OnColumn("TenancyName").Ascending()
                .OnColumn("UserNameOrEmailAddress").Ascending()
                .OnColumn("Result").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpUserLoginAttempts_UserId_TenantId").OnTable("dbo.AbpUserLoginAttempts")
                .OnColumn("UserId").Ascending()
                .OnColumn("TenantId").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpUserNotifications")
                .WithIdColumnGuid()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("UserId").AsInt64().NotNullable()
                .WithColumn("TenantNotificationId").AsGuid().NotNullable()
                .WithColumn("State").AsInt32().NotNullable()
                .WithColumn("CreationTime").AsDateTime().NotNullable();
            Create.Index("IX_dbo.AbpUserNotifications_UserId_State_CreationTime").OnTable("dbo.AbpUserNotifications")
                .OnColumn("UserId").Ascending()
                .OnColumn("State").Ascending()
                .OnColumn("CreationTime").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AbpUserOrganizationUnits")
                .WithIdColumnInt64()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("UserId").AsInt64().NotNullable()
                .WithColumn("OrganizationUnitId").AsGuid().NotNullable()
                .WithCreateColumns();
            Create.Index("IX_dbo.AbpUserOrganizationUnits_OrganizationUnitId").OnTable("dbo.AbpUserOrganizationUnits")
                .OnColumn("OrganizationUnitId").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpUserOrganizationUnits_TenantId_OrganizationUnitId").OnTable("dbo.AbpUserOrganizationUnits")
                .OnColumn("TenantId").Ascending()
                .OnColumn("OrganizationUnitId").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpUserOrganizationUnits_TenantId_UserId").OnTable("dbo.AbpUserOrganizationUnits")
                .OnColumn("TenantId").Ascending()
                .OnColumn("UserId").Ascending()
                .WithOptions().NonClustered();
            Create.Index("IX_dbo.AbpUserOrganizationUnits_UserId").OnTable("dbo.AbpUserOrganizationUnits")
                .OnColumn("UserId").Ascending()
                .WithOptions().NonClustered();

            Create.Table("dbo.AppBinaryObjects")
                .WithIdColumnGuid()
                .WithColumn("TenantId").AsInt64().Nullable()
                .WithColumn("Bytes").AsMaxBinary().NotNullable();
            #endregion

            //**************************ForeignKey********************************
            #region ForeignKey
            //Create.ForeignKey("FK_dbo.AbpAuditLogs_TenantId_AbpTenants")
            //      .FromTable("dbo.AbpAuditLogs").ForeignColumn("TenantId")
            //      .ToTable("dbo.AbpTenants").PrimaryColumn("Id");
            //Create.ForeignKey("FK_dbo.AbpAuditLogs_UserId_AbpUsers")
            //      .FromTable("dbo.AbpAuditLogs").ForeignColumn("UserId")
            //      .ToTable("dbo.AbpUsers").PrimaryColumn("Id");

            #endregion

            //**************************Initialize Data********************************
            #region Initialize Data

            #region Host seed
            //add a Standard edition
            Insert.IntoTable("dbo.AbpEditions").Row(new { Name = EditionManager.DefaultEditionName, DisplayName = EditionManager.DefaultEditionName });

            //add a default language list
            Insert.IntoTable("dbo.AbpLanguages").Row(new { Name = "en", DisplayName = "English", Icon = "famfamfam-flag-gb" })
                                                .Row(new { Name = "ar", DisplayName = "العربية", Icon = "famfamfam-flag-sa" })
                                                .Row(new { Name = "de", DisplayName = "German", Icon = "famfamfam-flag-de" })
                                                .Row(new { Name = "it", DisplayName = "Italiano", Icon = "famfamfam-flag-it" })
                                                .Row(new { Name = "pt-BR", DisplayName = "Portuguese", Icon = "famfamfam-flag-br" })
                                                .Row(new { Name = "tr", DisplayName = "Türkçe", Icon = "famfamfam-flag-tr" })
                                                .Row(new { Name = "zh-CN", DisplayName = "简体中文", Icon = "famfamfam-flag-cn" });

            //Admin role for host
            Insert.IntoTable("dbo.AbpRoles").Row(
                new { Name = StaticRoleNames.Host.Admin, DisplayName = StaticRoleNames.Host.Admin, IsStatic = true, IsDefault = true });

            //admin user for host
            Insert.IntoTable("dbo.AbpUsers").Row(
                new
                {
                    UserName = User.AdminUserName,
                    Name = "admin",
                    Surname = "admin",
                    EmailAddress = "admin@hollis.com",
                    IsEmailConfirmed = true,
                    ShouldChangePasswordOnNextLogin = true,
                    IsActive = true,
                    Password = "AM4OLBpptxBYmM79lGOX9egzZk3vIQU3d/gFCJzaBjAPXzYIK3tQ2N7X4fcrHtElTw==" //123qwe
                });

            //Assign Admin role to admin user
            Execute.Sql(@"insert into `dbo.abpuserroles`(UserId, RoleId)
select (select Id from `dbo.abpusers` where TenantId is null and UserName='admin' and IsActive=true and IsDeleted=false), 
(select Id from `dbo.abproles` where TenantId is null and Name='admin' and IsStatic=true and IsDefault=true and IsDeleted=false)");

            //Grant all permissions
            var permissions1 = PermissionFinder
                    .GetAllPermissions(new AppAuthorizationProvider())
                    .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Host))
                    .ToList();
            foreach (var permission in permissions1)
            {
                Execute.Sql(@"insert into `dbo.abppermissions`(Name, IsGranted, RoleId, Discriminator)
select '" + permission.Name + @"', 1,  
(select Id from `dbo.abproles` where TenantId is null and Name='admin' and IsStatic=true and IsDefault=true and IsDeleted=false),
'RolePermissionSetting'");
            }

            //User account of admin user
            Execute.Sql(@"insert into `dbo.abpuseraccounts`(UserId, UserName, EmailAddress)
select Id, '" + User.AdminUserName + @"', EmailAddress from `dbo.abpusers` where TenantId is null and UserName='admin' and IsActive=true and IsDeleted=false");

            //AbpSettings, add Emailing, Languages
            Insert.IntoTable("dbo.AbpSettings")
                //Emailing
                .Row(new { Name = EmailSettingNames.DefaultFromAddress, Value = "admin@mydomain.com" })
                .Row(new { Name = EmailSettingNames.DefaultFromDisplayName, Value = "mydomain.com mailer" })
                //Languages
                .Row(new { Name = LocalizationSettingNames.DefaultLanguage, Value = "en" });

            #endregion

            #region Default tenant seed (in host database).

            //Default tenant
            Execute.Sql(@"insert into `dbo.abptenants`(TenancyName, Name, EditionId)
select '" + MultiTenancy.Tenant.DefaultTenantName + @"','" + MultiTenancy.Tenant.DefaultTenantName + @"', 
(select Id from `dbo.abpeditions` where Name = '" + EditionManager.DefaultEditionName + @"' and IsDeleted=false)");

            //Admin role
            int tenantId = 1;
            Insert.IntoTable("dbo.AbpRoles")
                .Row(new { TenantId = tenantId, Name = StaticRoleNames.Tenants.Admin, DisplayName = StaticRoleNames.Tenants.Admin, IsStatic = true });

            //Grant all permissions to admin role
            var permissions2 = PermissionFinder
                .GetAllPermissions(new AppAuthorizationProvider())
                .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant))
                .ToList();
            foreach (var permission in permissions2)
            {
                Execute.Sql(@"insert into `dbo.abppermissions`(TenantId, Name, IsGranted, RoleId, Discriminator)
select " + tenantId + ", '" + permission.Name + @"', 1,  
(select Id from `dbo.abproles` where TenantId=" + tenantId + 
    " and Name='" + StaticRoleNames.Tenants.Admin + @"' and DisplayName = '" + StaticRoleNames.Tenants.Admin + 
    @"' and IsStatic = true and IsDefault=false and IsDeleted=false),
'RolePermissionSetting'");
            }

            //User role
            Insert.IntoTable("dbo.AbpRoles")
                .Row(new { TenantId = tenantId, Name = StaticRoleNames.Tenants.User, DisplayName = StaticRoleNames.Tenants.User, IsStatic = true, IsDefault = true });

            //admin user
            var adminUser = User.CreateTenantAdminUser(tenantId, "admin@defaulttenant.com", "123qwe");
            adminUser.IsEmailConfirmed = true;
            adminUser.ShouldChangePasswordOnNextLogin = true;
            adminUser.IsActive = true;
            Insert.IntoTable("dbo.AbpUsers")
                .Row(new
                {
                    TenantId = tenantId,
                    Name = User.AdminUserName,
                    SurName = User.AdminUserName,
                    UserName = User.AdminUserName,
                    Password = "AM4OLBpptxBYmM79lGOX9egzZk3vIQU3d/gFCJzaBjAPXzYIK3tQ2N7X4fcrHtElTw==", //123qwe
                    EmailAddress = adminUser.EmailAddress,
                    IsEmailConfirmed = adminUser.IsEmailConfirmed,
                    IsActive = adminUser.IsActive,
                    ShouldChangePasswordOnNextLogin = adminUser.ShouldChangePasswordOnNextLogin
                });

            //admin user -- Assign Admin role to admin user
            Execute.Sql(@"insert into `dbo.abpuserroles`(TenantId, UserId, RoleId)
select " + tenantId + @",  
(select Id from `dbo.abpusers` where TenantId=" + tenantId +
" and Name='" + User.AdminUserName + @"' and SurName = '" + User.AdminUserName + 
"' and UserName = '" + User.AdminUserName +
@"' and IsActive = true and IsDeleted=false),
(select Id from `dbo.abproles` where TenantId=" + tenantId +
    " and Name='" + StaticRoleNames.Tenants.Admin + @"' and DisplayName = '" + StaticRoleNames.Tenants.Admin +
    @"' and IsStatic = true and IsDeleted=false)
");

            //admin user -- User account of admin user
            Execute.Sql(@"insert into `dbo.abpuseraccounts`(TenantId, UserId, UserName, EmailAddress) 
select " + tenantId + @", Id, UserName, EmailAddress from `dbo.abpusers` where TenantId = " + tenantId +
" and Name='" + User.AdminUserName + @"' and SurName = '" + User.AdminUserName +
"' and UserName = '" + User.AdminUserName +
@"' and IsActive = true and IsDeleted=false");

            #endregion

            #endregion
        }
    }
}
